﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Library;
using System.Data;
using IHISCETeim.BLL;
using System.Text;


namespace IHISCETeim.Web.MachineSys.FTradeModel
{
    public partial class Report8FTrade : EasyUIPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "Report8FTrade";
            }
        }


        protected string GetDateFrom
        {
            get
            {
                return DateTime.Now.ToString("yyyy-MM-01");
            }
        }
        /// <summary>
        /// 上个月最后一天
        /// </summary>
        protected DateTime GetDateTo
        {
            get
            {
                return Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1);
            }
        }
        private DataTableCollection _dts = null;
        protected DataTableCollection dts
        {
            get
            {
                if (_dts == null)
                {
                    //营业经理权限 2014-02-21
                    string agentList = base.FTradeAgentPowerSqlWhere();
                    string wheresql = string.IsNullOrEmpty(agentList) ? " 1=1 " : string.Format(" JingXiaoShang in ({0})", agentList);

                    _dts = FTradeReportController.Report8GetReportDate(wheresql,GetDateTo.ToString("yyyy-MM-dd 23:59:59"));
                }
                return _dts;
            }
        }
        protected override string query()
        {
            //DataTableCollection dts = FTradeReceivablesController.GetPagination(GetSQLWhere(), pageNum, numPerPage, SQLSort, ref TotalCount);

            //帐龄信息加工
            DataTable DaoQiRidt = dts[0];
            DateTime minDaoQiRi;
            DateTime maxDaoQiRi;
            if (DaoQiRidt.Rows.Count <= 0)
            {
                minDaoQiRi = DateTime.Now;
                maxDaoQiRi = DateTime.Now;
            }
            else
            {
                minDaoQiRi = DaoQiRidt.Rows[0]["minDaoQiRi"] is DBNull ? DateTime.Now : Convert.ToDateTime(DaoQiRidt.Rows[0]["minDaoQiRi"]);
                maxDaoQiRi = DaoQiRidt.Rows[0]["maxDaoQiRi"] is DBNull ? DateTime.Now : Convert.ToDateTime(DaoQiRidt.Rows[0]["maxDaoQiRi"]);
            }
            //获取帐龄明细
            DataTable AgeDetailJinEDT = dts[2];
            //到期日列添加
            DataTable dtList = dts[1];
            DataTable dtSumList = dts[3];

            for (DateTime dtTemp = minDaoQiRi; dtTemp < maxDaoQiRi.AddMonths(1); dtTemp = dtTemp.AddMonths(1))
            {
                dtList.Columns.Add(dtTemp.ToString("yyyy-MM"), typeof(string));
                dtSumList.Columns.Add(dtTemp.ToString("yyyy-MM"), typeof(string));
            }
            //获取列值
            for (DateTime dtTemp = minDaoQiRi; dtTemp < maxDaoQiRi.AddMonths(1); dtTemp = dtTemp.AddMonths(1))
            {
                string startDT = dtTemp.ToString("yyyy-MM-01");
                string endDT = Convert.ToDateTime(dtTemp.AddMonths(1).ToString("yyyy-MM-01")).AddDays(-1).ToString("yyyy-MM-dd");
                foreach (DataRow dr in dtList.Rows)
                {
                    object JinE = AgeDetailJinEDT.Compute("sum(JinE)", string.Format(" JingXiaoShang='{0}' and DaoQiRi >= '{1}' and DaoQiRi<='{2}'", dr["经销商"], startDT, endDT));
                    dr[dtTemp.ToString("yyyy-MM")] = JinE;
                }
                object sumJinE = AgeDetailJinEDT.Compute("sum(JinE)", string.Format(" DaoQiRi >= '{0}' and DaoQiRi<='{1}'", startDT, endDT));
                dtSumList.Rows[0][dtTemp.ToString("yyyy-MM")] = sumJinE;
            }
            return ToDataGridJson(dtList, TotalCount, dtSumList).ToString();
        }

        #region 获取列JS代码
        private class GridColumnInfo
        {
            public string title { get; set; }
            public string field { get; set; }
            public int width { get; set; }
            public bool sortable { get; set; }
            public string formatter { get; set; }
        }

        protected string GetGirdColumns()
        {
            DataTable DaoQiRidt = dts[0];

            DateTime minDaoQiRi = Convert.ToDateTime(DaoQiRidt.Rows[0]["minDaoQiRi"]);
            DateTime maxDaoQiRi = Convert.ToDateTime(DaoQiRidt.Rows[0]["maxDaoQiRi"]);
            //maxDaoQiRi=maxDaoQiRi.AddMonths(1)
            List<GridColumnInfo> columnsList = new List<GridColumnInfo>();

            //到期日列添加
            for (DateTime dtTemp = minDaoQiRi; dtTemp < maxDaoQiRi.AddMonths(1); dtTemp = dtTemp.AddMonths(1))
            {
                GridColumnInfo gcItem = new GridColumnInfo();
                gcItem.title = dtTemp.ToString("yyyy-MM");
                gcItem.field = dtTemp.ToString("yyyy-MM");
                gcItem.width = 70;
                gcItem.sortable = false;
                gcItem.formatter = "milliformatter";

                columnsList.Add(gcItem);
            }

            StringBuilder sb = new StringBuilder();
            foreach (GridColumnInfo itm in columnsList)
            {
                sb.AppendFormat("{{ title: '{0}', field: '{1}', width: {2}, sortable: {3} {4}}},", itm.title, itm.field, itm.width, itm.sortable.ToString().ToLower(), string.IsNullOrEmpty(itm.formatter) ? "" : ",formatter:" + itm.formatter);
            }

            //返回COLUMNS JS
            return sb.ToString().Remove(sb.ToString().Length - 1, 1);



        }

        #endregion
    }
}